package org.stvd.repository.admin.impl;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.stvd.entities.admin.UserLogin;
import org.stvd.repository.admin.UserLoginDao;
import org.stvd.repository.base.impl.BaseDaoImpl;

@Repository("UserLoginDao")
public class UserLoginDaoImpl extends BaseDaoImpl<UserLogin> implements UserLoginDao {
    
    @Override
    public void deleteByUserId(String userId) {
        em.createQuery("delete from UserLogin T where T.userId = :userId")
        .setParameter("userId", userId)
        .executeUpdate();
        em.flush();
    }
    
    @Override
    public void deleteByIdType(String userId,String loginType) {
        em.createQuery("delete from UserLogin T where T.userId = :userId and T.loginType = :loginType")
        .setParameter("userId", userId)
        .setParameter("loginType", loginType)
        .executeUpdate();
        em.flush();
    }
    
    
    /**
     * 获取用户所有登陆账号
     */
    @Override
    public List<UserLogin> findUserLoginByUserId(String userId) {
        return findByHQL("from UserLogin T where T.userId= ?0", userId);
    }
    
    /**
     * 根据登陆账号获取
     */
    public UserLogin findUserLoginByLoginAccount(String loginAccount){
        String hql = "from UserLogin T where T.loginAccount= ?0";
        List<UserLogin> userLogins = findByHQL(hql, loginAccount);
        if(userLogins!=null && userLogins.size()>0) {
            return userLogins.get(0);
        }
        return null;
    }
    
    @Override
    /**
     * 根据登陆账号类型及登陆账号获取用户登陆账号信息
     */
    public UserLogin findUserLoginByTypeAccount(String loginType, String loginAccount){
        String hql = "from UserLogin T where T.loginType= ?0 and T.loginAccount = ?1";
        List<UserLogin> userLogins = findByHQL(hql, loginType, loginAccount);
        if(userLogins!=null && userLogins.size() > 0) {
            return userLogins.get(0);
        }
        return null;
    }
    
    /**
     * 根据userID及账号类型登陆账号信息
     */
    public UserLogin findUserLoginByIDType(String userId, String loginType){
        String hql = "from UserLogin T where T.userId= ?0 and T.loginType = ?1";
        List<UserLogin> userLogins = findByHQL(hql, userId, loginType);
        if(userLogins!=null && userLogins.size()>0) {
            return userLogins.get(0);
        }
        return null;
    }
}
